package com.owen.system.keep.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.owen.common.utils.ShiroUtils;
import com.owen.common.utils.SnowflakeIdWorker;
import com.owen.system.keep.account.dto.KeepAccountDTO;
import com.owen.system.keep.account.dao.KeepAccountDao;
import com.owen.system.keep.account.entity.KeepAccount;
import com.owen.system.keep.account.service.KeepAccountService;
import com.owen.system.keep.account.vo.KeepAccountVO;
import com.owen.system.keep.consumption.dao.KeepConsumptionTypeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class KeepAccountServiceImpl implements KeepAccountService {

    @Autowired
    private KeepAccountDao keepAccountDao;

    // 收支类型
    @Autowired
    private KeepConsumptionTypeDao keepConsumptionTypeDao;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Override
    public KeepAccountVO get(String id) {
        return keepAccountDao.get(id);
    }

    @Override
    public List<KeepAccountVO> list(Map<String, Object> map) {
        String userId = ShiroUtils.getUserId();
        Assert.isTrue(ObjectUtil.isNotEmpty(userId), "请登录");
        map.put("createBy", userId);
        return keepAccountDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        String userId = ShiroUtils.getUserId();
        Assert.isTrue(ObjectUtil.isNotEmpty(userId), "请登录");
        map.put("createBy", userId);
        return keepAccountDao.count(map);
    }

    @Override
    public int save(KeepAccountDTO keepAccountDTO) {
        String userId = ShiroUtils.getUserId();
        Assert.isTrue(ObjectUtil.isNotEmpty(userId), "请登录");
        KeepAccount keepAccount = BeanUtil.copyProperties(keepAccountDTO, KeepAccount.class);
        keepAccount.setId(snowflakeIdWorker.nextIdStr());
        keepAccount.setCreateTime(new Date());
        keepAccount.setCreateBy(userId);
        return keepAccountDao.save(keepAccount);
    }

    @Override
    public int update(KeepAccountDTO keepAccountDTO) {
        String userId = ShiroUtils.getUserId();
        Assert.isTrue(ObjectUtil.isNotEmpty(userId), "请登录");
        KeepAccount keepAccount = BeanUtil.copyProperties(keepAccountDTO, KeepAccount.class);
        return keepAccountDao.update(keepAccount);
    }

    @Override
    public int remove(String id) {
        int keepConsumptionTypeCount = keepConsumptionTypeDao.findCountByKeepAccountId(id);
        Assert.isTrue(keepConsumptionTypeCount  <= 0, "该账户下存在收支类型，无法删除");
        return keepAccountDao.remove(id);
    }

    @Override
    public int batchRemove(String[] ids) {
        int keepConsumptionTypeCount = keepConsumptionTypeDao.findCountByKeepAccountIds(ids);
        Assert.isTrue(keepConsumptionTypeCount  <= 0, "选中要删除账户中存在收支类型，无法批量删除");
        return keepAccountDao.batchRemove(ids);
    }

    @Override
    public List<KeepAccountVO> findMyAccount() {
        String userId = ShiroUtils.getUserId();
        Assert.isTrue(ObjectUtil.isNotEmpty(userId), "请登录");
        return keepAccountDao.findByCreateBy(userId);
    }

}
